<?php
/**
 * Created by PhpStorm.
 * User: Guan
 * Date: 2018/3/26
 * Time: 16:12
 */

namespace app\admin\controller;


use think\Request;
use app\admin\logic\Banji as Banji_logic;
class Banji extends Admin
{
    public function index()
    {
        $banji = new Banji_logic;
        $data = $banji->getbanjiall();
        if (empty($data)) {
            $data = '数据丢失';
        } else {
        }

        $this->assign('banji', $data);
        return $this->fetch();
    }

    public function add()
    {
        $Banji = new Banji_logic();
        if ($this->request->isGet()){
            return $this->fetch();
        }elseif ($this->request->isPost()){
            $data = $this->request->post();
            if (empty($data)){
                $this->error('数据丢失');
            }
            unset($data['__token__']);
            $data['time'] = explode('-',$data['time']);
            foreach ($data['time'] as $k=>$v){
                $data['time'][$k] = strtotime(date('Y-m-d '.$v));
            }
            $data['time'] = implode(',',$data['time']);
            $data['create_time'] = time();
            $res = $Banji->addBanji($data);
            if ($res['result']!= false){
                $this->success($res['msg']);
            }else{
                $this->error($res['msg']);
            }
        }
    }

    public function edit(Request $request, $id = '')
    {
        $Banji = new Banji_logic();
        if ($this->request->isGet()) {
            $data = db('banji')->where('id', $id)->find();
            $timearr = explode(',', $data['time']);
            foreach ($timearr as $k => $v) {
                $timearr[$k] = date('H:s', $v);
            }
            $timearr = implode('-', $timearr);
            $data['time'] = $timearr;
            $this->assign('banji', $data);
            return $this->fetch();
        } elseif ($this->request->isPost()) {
            $res = $request->post();
            unset($res['__token__']);
            $timearr = explode('-', $res['time']);
            foreach ($timearr as $k => $v) {
                $timearr[$k] = strtotime(date('Y-m-d ') . ' ' . $v);
            }
            $res['time'] = implode(',', $timearr);
            $result = $Banji->editBanji($res);
            if ($result['result']!= false){
                $this->success($result['msg']);
            }else{
                $this->error($result['msg']);
            }

        }

    }

    public function del($id = '')
    {
        $id = $this->request->param('id', 0);
        $Origin_logic = new Banji_logic();
        $result = $Origin_logic->delBanji($id);
        if ($result['result']!= false){
            $this->success($result['msg']);
        }else{
            $this->error($result['msg']);
        }
    }

}